<?
/**
 * VoiceOne Settings/System/CLI
 *
 * Frontend to provide access to the system Command Line Interface  
 * @package voiceone
 * @subpackage  settings_system_cli
 */
 
  include ("../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
  require_once("$absBasePath/lib/xajax/xajax.inc.php");
  
  //================ XAJAX ================
  function dropdownmenu($commandList,$entered,$offsetLeft,$offsetTop) {
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Build response
    $offsetTop+=22;
    $i=0;
    $opt="";
    foreach($commandList as $command){
      if($entered==substr($command,0,strlen($entered))){
        $opt.="<option value=\"$command\" > $command </option>";
        $i++;
      }
    }
    if($i==0) $html="";
    else if($i==1) $html="<select id=\"selectList\" size=\"2\" onchange=\"document.getElementById('command').value=this.value;\" onkeyup=\"myHandler(event);\" >$opt</select>";
    else if($i<5) $html="<select id=\"selectList\" size=\"$i\" onchange=\"document.getElementById('command').value=this.value;\" onkeyup=\"myHandler(event);\" >$opt</select>";
    else $html="<select id=\"selectList\" size=\"5\" onchange=\"document.getElementById('command').value=this.value;\" onkeyup=\"myHandler(event);\" >$opt</select>"; 
    // Return the xajaxResponse object
    $objResponse->addAssign("tooltip","innerHTML",$html);
    return $objResponse->getXML();  
  }
  function sendCliCommand($command){
    global $relBasePath, $asterisk;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Build response
    $ret=$asterisk->sendCommand($command);
    $i=0;
    $text="VoiceOne*CLI>".$command."\n";
    $retArr=split("\n",$ret);
    error_log($ret);
    foreach($retArr as $line) if(($i++>1)&&($line!="--END COMMAND--\r")) $text.=$line."\n";
    // Return the xajaxResponse object
    $objResponse->addAppend("clitext","value",$text);
    $objResponse->addAssign("command","value",'');
    $objResponse->addAssign("tooltip","innerHTML",'');
    $objResponse->addScript(" document.getElementById('clitext').scrollTop = document.getElementById('clitext').scrollHeight; ");
    return $objResponse->getXML();  
  }  
  $xajax = new xajax();
  $xajax->registerFunction("sendCliCommand");
  $xajax->registerFunction("dropdownmenu");
  $xajax->processRequests();
  
  //================= PAGE LOGIC ==================
  // generate Command List
  $status=$asterisk->pbxSwitch("status");
  if($status=="ON"){
    $ret=$asterisk->sendCommand("help");
    $retArr=split("\n",$ret);
    $commandNum=0;
    foreach($retArr as $line){
      if($line=="--END COMMAND--\r") break;
      if($commandNum++>1) {
        $line=str_replace("\r","",$line);
        $ris=eregi("^[^[:alpha:]]*(.*)[[:space:]]{2}(.*)$", $line, $command);
        if($command[1]!="") $commandList[]=$command[1];
      }
    $startScript="<script> document.getElementById('command').focus(); </script>";
    }
  }else{
    $commandList=array();
    $startScript="<script> document.getElementById('send').disabled='disabled'; document.getElementById('command').disabled='disabled'; </script>";
  }

  //================ MAIN CONTENTS ================
  ob_start();
  $xajax->printJavascript("../../../admin/lib/xajax/"); ?>
  
   <?$sel="cli";include("../navigation.php");?> 
  
  <div id="firstpoint"></div>
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=T_("CLI (Command Line Interface)")?></h1>
  </div>

  <!-- CLI -->
  <div id="basic_form">
    <h2></h2>
    <p class="blockintro"><?=T_("Enter PBX command")?></p>
    <div class="block" align="center">
      <textarea id="clitext" readonly="readonly" name="clitext" rows="24" wrap="off" style="color: white; background: black; margin-top: 5px; width: 95%; " ></textarea>
      <dl>
        <dt class="required"><?=T_("Command")?>:</dt>
          <dd>
            <input type="textfield" style="width: 65%" id="command"  name="command" value="" onkeyup=" myHandler(event); "  />
            <div id="tooltip" style="z-index: 100; position: relative; left: 120px; float: left;"></div>
            <input type="button" id="send" name="send" value="Send" onclick=" xajax_sendCliCommand(document.getElementById('command').value); document.getElementById('tooltip').innerHTML=''; " />
            <input type="button" id="clear" name="clear" value="Clear" onclick=" document.getElementById('clitext').value=''; " />
          </dd>
      </dl>
    </div>
  </div>
  
  <div id="tooltip2" style="position: absolute; top: 0; left: 0; z-index: 100"></div>
  <?=$startScript?>
  <script> 
    document.getElementById('clitext').scrollTop = document.getElementById('clitext').scrollHeight;
    //build array of commands
    var cmdList = new Array();
    <? foreach($commandList as $command) print "cmdList.push(\"$command\" );"; ?>
    //handler
    function myHandler(evt){
      evt = (evt) ? evt : ((event) ? event : null);
      if(evt){
        var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
        if(elem){
          if((elem.id=="command")){
            //alert(evt.keyCode);
            if(evt.keyCode==13){
              xajax_sendCliCommand(document.getElementById('command').value);
            }else if(evt.keyCode==40){
              var selectObj = document.getElementById('selectList');
              if(selectObj){
                selectObj.focus();
                selectObj.options[0].selected="selected";
              }
            }else if((evt.keyCode==8)||(evt.keyCode==32)||((evt.keyCode>64)&&(evt.keyCode<91))){
              xajax_dropdownmenu(cmdList, elem.value, elem.offsetLeft, elem.offsetTop);
            }
          }
          else if(elem.id=="selectList"){
            if(evt.keyCode==13){
              var commandObj = document.getElementById('command');
              var tooltipObj = document.getElementById('tooltip');
              if(commandObj){
                commandObj.value=elem.value;
                commandObj.focus();
              }   
              if(tooltipObj) tooltipObj.innerHTML="";

            }
          }  
        }
      }    
    }
  </script>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
    <div class="Sidebar">
      <h1><?=T_("information")?></h1>
          <p><?=T_("The Asterisk Command Line Interface (CLI) has many commands which you can run to diagnose and maintain your system. These commands can be used to show information about the current state of the system. The Asterisk CLI works very similar to many managed switches and routers CLI interfaces.")?></p>
    </div>    
           
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================

  $smarty->assign("section", "system");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

